package zombiegame;

import java.util.Iterator;
import java.util.List;

import graphics.*;

/**
 * Vampire class, derives from Character.
 * @author pylaffon
 *
 */
public class Vampire extends Character {

    private boolean isThirsty;

    public Vampire(String name, int healthPoints, Field field, Location location) {
        super(name, healthPoints, field, location, TypeCharacter.Vampire);
        isThirsty = false;
    }

    // Accessors and mutators
    public boolean getIsThirsty() {
        return isThirsty;
    }
    public void setIsThirsty(boolean isThirsty) {
        this.isThirsty = isThirsty;
    }
    /**
     * Method triggered on each character at the end of each turn.
     */
    public void endOfTurn() {
        // The vampire has 50% chance of becoming thirsty, if he is not already

        if (isThirsty || Simulator.GenerateRandomBoolean()) {
            isThirsty = true;
            //say("I am thirsty now!!");
        }

        Location newLocation = findHuman(location);
        if (newLocation != null) {
            Character c = (Character)field.getObjectAt(newLocation);
            this.encounterCharacter(c);
            if (c.getType() == TypeCharacter.Human && this.isThirsty) {
                this.bite((Human) c);
            }
            newLocation = null;
        }
        
        if (newLocation == null) {
            // No food found - try to move to a free location.
            newLocation = field.freeAdjacentLocation(location);
        }
        // See if it was possible to move.
        if (newLocation != null) {
            setLocation(newLocation);
        } else {
            // Overcrowding.
            setDead();
        }
        
    }

    /**
     * Tell the fox to look for rabbits adjacent to its current location. Only
     * the first live rabbit is eaten.
     * 
     * @param location
     *            Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
     */
    private Location findHuman(Location location) {
        List<Location> adjacent = field.adjacentLocations(location);
        Iterator<Location> it = adjacent.iterator();
        while (it.hasNext()) {
            Location where = it.next();
            Object object = field.getObjectAt(where);
            if (object instanceof Character) {
                Character character = (Character) object;
                if (character.getType() == TypeCharacter.Human) {
                    return where;
                }
            }
        }
        return null;
    }



    /**
     * Method called when a vampire decides to bite a human
     * @param h Human who gets bitten by this vampire
     */
    public void bite(Human h) {
        // The human has no way to escape. He gets bitten.
        h.setHasBeenBitten(true);
        //say("I have bitten you, " + h.getName() + "!");
        // Vampire is not thirsty anymore
        isThirsty = false;
    }

    /**
     * Method called when a vampire decides to attack a character
     * @param c the other character that this vampire attacks
     */
    protected void attack(Character c) {
        c.reduceHealthPoints(10);
    }

    /**
     * Method triggered when the vampire described by the current object
     * meets another character, and does something to him (for example, attack).
     * @param c the other character that this character meets
     */
    public void encounterCharacter(Character c) {
        // Vampires always attack 
        //say(c.getName() + ", I'm gonna kill you!!");
        attack(c);
    }

}
